using System;
using System.Collections.Generic;
using System.Linq;
using WeChatCmsCommon.EnumBusiness;
using WeChatDataAccess;
using WeChatModel.DatabaseModel;

namespace WeChatService
{
    /// <summary>
    /// 项目服务
    /// </summary>
    public class ProjectService
    {
        /// <summary>
        /// 数据服务
        /// </summary>
        private ProjectData _dataAccess = new ProjectData();
        /// <summary>
        /// 获取项目列表
        /// </summary>
        /// <param name="userIds">用户ID列表</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="count">总数</param>
        /// <param name="name">项目名称（可选）</param>
        /// <returns></returns>
        public List<ProjectModel> GetList(List<long> userIds, int pageIndex, int pageSize, out int count, string name = "")
        {
            count = _dataAccess.GetCount(userIds, name);
            return _dataAccess.GetModels(userIds, pageIndex, pageSize, name);
        }

        /// <summary>
        /// 根据ID获取项目
        /// </summary>
        /// <param name="id">项目ID</param>
        /// <returns></returns>
        public ProjectModel Get(long id)
        {
            if (id < 1) return null;
            var data = _dataAccess.Get(id);
            if (data == null || data.IsDel == FlagEnum.HadOne) return null;
            return data;
        }

        /// <summary>
        /// 添加项目
        /// </summary>
        /// <param name="model">项目模型</param>
        /// <returns>项目ID</returns>
        public long Add(ProjectModel model)
        {
            if (model == null) return 0;
            model.Id = 0; // 确保是添加操作
            return _dataAccess.SaveModel(model);
        }

        /// <summary>
        /// 更新项目
        /// </summary>
        /// <param name="model">项目模型</param>
        /// <returns></returns>
        public bool Update(ProjectModel model)
        {
            if (model == null || model.Id < 1) return false;
            var result = _dataAccess.SaveModel(model);
            return result > 0;
        }

        /// <summary>
        /// 检查项目名称是否存在
        /// </summary>
        /// <param name="projectName">项目名称</param>
        /// <param name="userId">用户ID</param>
        /// <param name="excludeId">排除的项目ID（编辑时使用）</param>
        /// <returns></returns>
        public bool IsProjectNameExists(string projectName, long userId, long? excludeId = null)
        {
            return _dataAccess.IsProjectNameExists(projectName, userId, excludeId);
        }

        /// <summary>
        /// 删除项目
        /// </summary>
        /// <param name="id">项目ID</param>
        public void DelModel(long id)
        {
            _dataAccess.DelModel(id);
        }

        /// <summary>
        /// 保存项目信息
        /// </summary>
        /// <param name="model">项目模型</param>
        /// <returns></returns>
        public long SaveModel(ProjectModel model)
        {
            if (model == null) return 0;
            return _dataAccess.SaveModel(model);
        }
    }
}